package com.vsu.bookstore.mvc.ui.widgets;

import java.text.DecimalFormat;

import com.vaadin.ui.Label;
import com.vsu.bookstore.mvc.model.bean.BookStoreItem;
import com.vsu.bookstore.util.CurrencyExchangeProvider;

@SuppressWarnings("serial")
public class EuroToRubleLabel extends Label
{
  public EuroToRubleLabel()
  {
    super();
    setSizeUndefined();
  }
  @Override
  public void setValue(final Object newValue)
  {
    if (newValue instanceof Double ||
        newValue instanceof BookStoreItem)
    {
      final DecimalFormat format = new DecimalFormat();
      format.setMaximumFractionDigits(2);
      Double decVal = newValue instanceof Double ?
          ((Double)newValue) : ((BookStoreItem)newValue).getPrice();
      decVal *= CurrencyExchangeProvider.getEuroToRubleRatio();
      String result = "" + format.format(decVal) + " rub";
      if (newValue instanceof BookStoreItem)
        result = "" + ((BookStoreItem)newValue).getQuantity() + "x" + result;
      super.setValue(result);
    }
    else
      super.setValue(newValue);
  }
}
